package edu.cqie.smartfarm.controller;


import edu.cqie.smartfarm.constant.CommonConstant;
import edu.cqie.smartfarm.dto.BarnDTO;
import edu.cqie.smartfarm.dto.BarnQueryDTO;
import edu.cqie.smartfarm.dto.BarnUpdateDTO;
import edu.cqie.smartfarm.result.PageVO;
import edu.cqie.smartfarm.result.R;
import edu.cqie.smartfarm.service.IBarnService;
import edu.cqie.smartfarm.vo.BarnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yyw
 * @since 2024-12-26
 */
@RestController
@RequestMapping("/barn")
@RequiredArgsConstructor // 自动注入
@Api(tags = "圈舍管理")
public class BarnController {
    private final IBarnService barnService;

    /**
     * 获取圈舍列表
     */
    @ApiOperation(value = "获取圈舍列表")
    @GetMapping("/list")
    @Cacheable(value = CommonConstant.PROJECT_NAME+":barnList", key = "'page_'+#barnQueryDTO.pageNumber+'_size_'+#barnQueryDTO.pageSize+'_query_'+#barnQueryDTO")
    public R<PageVO<BarnVO>> getList(@Validated BarnQueryDTO barnQueryDTO){
        PageVO<BarnVO> pageVO = barnService.getList(barnQueryDTO);
        return R.Success(pageVO);
    }

    /**
     * 添加圈舍
     */
    @ApiOperation(value = "添加圈舍")
    @PostMapping("/add")
    @CacheEvict(value = CommonConstant.PROJECT_NAME+":barnList", allEntries = true)
    public R addBarn(@RequestBody @Validated BarnDTO barnDTO){
        barnService.addBarn(barnDTO);
        return R.Success();
    }

    /**
     * 修改圈舍
     */
    @ApiOperation(value = "修改圈舍")
    @PutMapping("/update")
    @CacheEvict(value = CommonConstant.PROJECT_NAME+":barnList", allEntries = true)
    public R updateBarn(@RequestBody @Validated BarnUpdateDTO barnUpdateDTO){
        barnService.updateBarn(barnUpdateDTO);
        return R.Success();
    }
}
